const fs = require('fs');
const path = require('path');
var tools = require('../../../libs/tools');
var config = require(tools.rootPath + 'config');
var { connection, pool, model } = require(tools.rootPath + 'libs/model');

var controller = require(tools.rootPath + 'libs/controller.js');

var article_model = require(tools.modelPath + 'article.js');
class index extends controller {
    constructor() {
        super();
    }
    __call(req, res) {
        console.log(tools.tableCachePath);
        return req.MM + '/' + req.CC + '/' + req.AA;
    }
    index(req, res) {
        //console.log(article_model.demo());
        req.session.USERID = 1000;
        console.log(tools.viewPath);
        res.render(req.TPL, { title: 'Express' });
    }
    tpl_demo(req, res) {
        res.end(this.tpl('index/index/tpl_demo'));
    }
    msg_test(req, res) {
        res.end(tools.msg(1, 'success', '/home/index/index'));
    }
    moment_test(req, res) {
        //unix to ymdhms
        //return tools.moment(1411641720000).format("YYYY-MM-DD HH:mm:ss");

        res.end(parseInt(new Date('2019-05-05 10:00:00').getTime() / 1000).toString());
    }
    async upload_test(req, res) {
        if (req.ispost) {
            if (!req.files || Object.keys(req.files).length === 0) {
                return this.msg(0, '请选择一个文件');
            }
            console.log(req.body);
            let cls = '';
            let i = 0;
            let callback = '';
            let uniq = '';

            let extensions = [
                'jpg',
                'png',
                'gif',
            ];


            let file = req.files.sampleFile;
            /*
            {
                "name":"ma01.jpg",
                "data":{"type":"Buffer","data":[]},
                "size":768420,
                "encoding":"7bit",
                "tempFilePath":"D:\\tmp\\tmp-1-1586834146916",
                "truncated":false,
                "mimetype":"image/jpeg",
                "md5":"d70e4c45a4ffb1aea17b1441c13a9a05"
            }
            */

            //判断文件大小
            let size = file.size;
            if (size > 2 * 1024 * 1024) {
                return this.json({ result: { 'code': 101, 'msg': '文件超出最大大小', 'url': '', 'class': cls, 'i': i, 'callback': callback } });
            }
            //判断文件后缀
            let filename = file.name;
            let ext = tools.extension(filename);
            if (!tools.in_array(ext, extensions)) {
                return this.json({ result: { 'code': 102, 'msg': '文件类型不正确', 'url': '', 'class': cls, 'i': i, 'callback': callback } });
            }
            //创建文件夹
            let destination = tools.uploadsPath + tools.date('', 'Y/M/D') + '/' + tools.date('', 'YMDhms') + tools.randomNum(100000, 999999) + '.' + ext;
            if (!fs.existsSync(path.dirname(destination))) {
                fs.mkdirSync(path.dirname(destination), { recursive: true });
            }
            //复制文件
            let url = destination.replace(tools.rootPath + 'public', '');
            fs.copyFileSync(file.tempFilePath, destination);
            if (!fs.existsSync(destination)) {
                return this.json({ result: { 'code': 103, 'msg': '文件写入失败', 'url': '', 'class': cls, 'i': i, 'callback': callback } });
            }

            return this.json({ result: { code: 0, msg: 'success', url: url, class: cls, i: i, callback: callback } });
        }
        res.render(req.TPL, {});
    }
    session_test(req, res) {

        // res.json(req.session['USERID']);
        // res.json(req.session['CAPTCHA']);
        return JSON.stringify(req.session);
    }


}

module.exports = index;